package com.huluobo.high3demo.mine;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.viewpager2.widget.ViewPager2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.SPUtils;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.huluobo.high3demo.R;
import com.huluobo.high3demo.adapter.CommonPagerAdapter;
import com.huluobo.high3demo.databinding.FragmentMineBinding;
import com.huluobo.high3demo.entity.TabEntity;
import com.huluobo.high3demo.mine.item.WorksFragment;
import com.huluobo.high3demo.setting.SettingActivity;
import com.huluobo.mvvm.db.eneity.UserInfo;
import com.huluobo.mvvm.net.Constant;
import com.huluobo.mvvm.utils.ImageLoader;
import com.huluobo.mvvm.view.BaseMVVMFragment;

import java.util.ArrayList;
import java.util.List;

public class MineFragment extends BaseMVVMFragment<FragmentMineBinding, MineViewModel> {
    private UserInfo mUserInfo;
    private ArrayList<CustomTabEntity> tabEntities = new ArrayList<>();

    private CommonPagerAdapter commonPagerAdapter;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initData() {
        viewModel.userInfoSuccess.observe(this, new Observer<UserInfo>() {
            @Override
            public void onChanged(UserInfo userInfo) {
                mUserInfo = userInfo;
                //拿到用户信息,赋值头像和昵称
                ImageLoader.getInstance().loadCircleImage(getActivity(), userInfo.icon, dataBinding.mineIvUserIcon);
                dataBinding.mineTvNickname.setText(userInfo.username);
            }
        });
    }

    @Override
    protected void initView() {
        viewModel.queryUserInfo(SPUtils.getInstance().getString(Constant.SP_USERNAME));

        tabEntities.clear();
        tabEntities.add(new TabEntity("作品", 0, 0));
        tabEntities.add(new TabEntity("私密", 0, 0));
        tabEntities.add(new TabEntity("喜欢", 0, 0));
        tabEntities.add(new TabEntity("收藏", 0, 0));
        dataBinding.mineTabLayout.setTabData(tabEntities);

        fragments.clear();
        fragments.add(new WorksFragment());
        fragments.add(new WorksFragment());
        fragments.add(new WorksFragment());
        fragments.add(new WorksFragment());

        commonPagerAdapter = new CommonPagerAdapter(getActivity(), fragments);
        dataBinding.mineVp2.setAdapter(commonPagerAdapter);

        //设置tab与vp进行联动
        dataBinding.mineTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                dataBinding.mineVp2.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        //设置VP与tab进行联动
        dataBinding.mineVp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                dataBinding.mineTabLayout.setCurrentTab(position);
            }
        });

        dataBinding.mineIvSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected Class<MineViewModel> getViewModel() {
        return MineViewModel.class;
    }
}